﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Net.Sockets;
using V5_Book.Model;
using System.Text;
using V5_PhoneLibs;

namespace V5_Book.View.GetResource {
    public partial class PageGetSocket : PhoneApplicationPage {

        public PageGetSocket() {
            InitializeComponent();
        }

        private SocketClient _client = new SocketClient();

        private void btnLinkIP_Click(object sender, RoutedEventArgs e) {
            string ip = this.TbIp.Text;
            _client = new SocketClient();
            var sbText = new StringBuilder();
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            var response = _client.Connect(ip, 4530);
            _client.Osd = (string fileTitle, string content) => {
                    fileName = fileTitle;
                    string s = content;
                    List<ModelBookInfo> listBook = BookHelper.Instance.ListBookInfo();
                    BookHelper.Instance.AddBookInfo(fileName + ".txt", s);
                    listBook.Add(new ModelBookInfo() {
                        BookName = fileName,
                        BookIndex = 1,
                        LastTime = DateTime.Now,
                        CreateTime = DateTime.Now
                    });
                    BookHelper.Instance.SaveListBookInfo(listBook, null);
                    OutMessage("接收完成!");
                };
            var response1 = _client.Receive();
            this.TbShowResult.Text = response;
        }

        private void OutMessage(string msg) {
            this.Dispatcher.BeginInvoke(() => {
                this.TbShowResult.Text = msg;
            });
        }
    }
}